package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.ShopCartListDTO;
import cn.ulc.socket.weapp.model.param.ShopCartParam;
import cn.ulc.socket.weapp.service.ShopCartService;
import cn.ulc.store.dao.goods.GoodsSpecDao;
import cn.ulc.store.dao.user.MemberDao;
import cn.ulc.store.dao.user.ShopCartDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.goods.Goods;
import cn.ulc.store.entity.goods.GoodsSpec;
import cn.ulc.store.entity.user.Member;
import cn.ulc.store.entity.user.ShopCart;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ShopCartServiceImpl extends BaseService implements ShopCartService {

    ShopCartDao shopCartDao;
    MemberDao memberDao;
    GoodsSpecDao specDao;


    @Override
    public ShopCartListDTO findList() {
        WeixinUser loginUser = getCurrentUser();
        List<ShopCart> list = shopCartDao.findShopCart(loginUser.getId());
        return new ShopCartListDTO(list);
    }

    @Override
    public Boolean save(ShopCartParam param) {
        WeixinUser loginUser = getCurrentUser();
        GoodsSpec goodsSpec = specDao.findById(param.getSkuId());
        if(goodsSpec==null) throw new PlatformException("获取商品规格信息失败!");
        Goods goods = goodsSpec.getGoods();
        ShopCart shopCart = shopCartDao.findByGoodsIdAndSpecId(loginUser.getId(),goods.getId(),goodsSpec.getId());
        if(shopCart ==null){
            shopCart = new ShopCart();
            setBaseInfo(shopCart);
            shopCart.setGoodsId(goods.getId());
            shopCart.setGoodsName(goods.getName());
            shopCart.setGoodsTypeId(goods.getGoodsTypeId());
            shopCart.setGoodsSpecId(goodsSpec.getId());
            shopCart.setGoodsSpecName(goodsSpec.getName());
            shopCart.setGoodsPrice(goodsSpec.getPrice());
            shopCart.setAmount(param.getCount().multiply(goodsSpec.getPrice()));
            shopCart.setCompanyId(loginUser.getCompanyId());
            shopCart.setWxUserId(loginUser.getId());
            shopCart.setNum(param.getCount());
            Member member = memberDao.findByWxUserId(loginUser.getId(),AvailableStatus.AVAILABLE);
            if(member!=null) shopCart.setMemberId(member.getId());
        }else{
            setBaseInfo(shopCart);
            shopCart.setNum(shopCart.getNum().add(param.getCount()));
        }
        shopCartDao.save(shopCart);
        return true;
    }

    @Override
    public Boolean changeNum(ShopCartParam param) {
        ShopCart shopCart = shopCartDao.findOne(param.getId());
        if(shopCart == null) throw new PlatformException("服务器繁忙，请求失败");
        setBaseInfo(shopCart);
        shopCart.setNum(param.getCount());
        shopCartDao.save(shopCart);
        return true;
    }

    @Override
    public Boolean deleted(Long id) {
        ShopCart shopCart = shopCartDao.findOne(id);
        if(shopCart != null) {
            setBaseInfo(shopCart);
            shopCart.setDeleted(DeletedStatus.DELETED);
            shopCartDao.save(shopCart);
        }
        return true;
    }
}
